import axios from "axios";
import { Notification, Message } from "element-ui";
import store from "@/store";
import { getToken } from "@/utils/auth";

import Cookies from 'js-cookie'

import zztoken from './zztoken'



// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  baseURL: "/minyong",
  timeout: 50000 // request timeout
});

// request interceptor
service.interceptors.request.use(
  config => {
    if (store.getters.token) {

      config.headers["Authorization"] = 'Bearer ' + Cookies.get("dptoken")

    }
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  response => {
    if (response.data.code != 200) {
      zztoken().then()
    }
    if (response.config.url.indexOf('bigScreen') != -1) {
      return response.data
    }
    if (response.config.url.indexOf('connectorlist') != -1) {
      return response.data
    }
    if (response.config.url.indexOf('warn') != -1) {
      return response.data
    }
    if (response.config.url.indexOf('heatPlants') != -1) {
      const res = response.data;
      res['data'] = res.rows
      return res
    }
    if (response.config.url.indexOf('queryDb') != -1) {
      const res = response.data;
      response.data["200"] = res.data
      // res[200] = res.data
      // console.log(res);
      return response
    }
    const res = response.data;
    return res;
  },
  error => {
    console.log("err" + error); // for debug
    Notification({
      message: error.message
    });
    return Promise.reject(error);
  }
);

export default service;
